// pages/order/order.js

import {
  Request
} from '../../http/http.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 提交订单的数据
    orderList: [],
    // 总价
    total: 0,
  },

  // 提交表单
  onSubmit() {
    // 获取token
    let token = wx.getStorageSync('token')
    // 没有token就跳转到登陆页面
    if (!token) {
      wx.reLaunch({
        url: '/pages/login/login',
      })
      return;
    }
    // 获取它要的数据
    let goodsJson = this.data.orderList.map(item => {
      return {
        goodsId: item.goodsId,
        logisticsType: 0,
        number: item.count,
        propertyChildIds: item.id,
        checked: item.checked
      }
    })
    // 它想要的是json格式  转成json格式
    goodsJson = JSON.stringify(goodsJson)
    // // 删除购车中已结算的数据
    let cart = wx.getStorageSync('cart') || []
    let orderlist = cart.filter(item => {
      return item.checked == false
    })
    wx.setStorageSync('cart', orderlist)
    Request({
      url: 'order/create',
      data: {
        token,
        goodsJsonStr: goodsJson
      },
      method: "POST"
    }).then(res => {
console.log(res);
      let {
        amount,
        orderNumber
      } = res.data
 
      // 跳转支付页面
      wx.navigateTo({
        url: '/pages/payment/payment?amount=' + amount + "&orderNumber=" + orderNumber,
      })

    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    let order = wx.getStorageSync('order')
    this.data.total = order.reduce((total, item) => {
      return total += (item.minPrice * item.count) * 100
    }, 0)
    this.setData({
      orderList: order,
      total: this.data.total
    })
    console.log(this.data.orderList);
  },













})